package com.wbw.ssb.controller;

import com.wbw.ssb.common.api.CommonPage;
import com.wbw.ssb.common.api.CommonResult;
import com.wbw.ssb.dto.UserParam;
import com.wbw.ssb.mbg.entity.User;
import com.wbw.ssb.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: guibo
 * @Description:
 * @Date: Created in 0:35 2021/5/19
 * @Modified By:
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @ApiOperation("登录")
    @PostMapping(value = "/login")
    public CommonResult<String> login(@RequestParam String tel, @RequestParam String password) {
        return CommonResult.success(userService.login(tel,password));
    }

    @ApiOperation("注册")
    @PostMapping(value = "/register")
    public CommonResult<Integer> register(@RequestBody UserParam userParam) {
        return CommonResult.success(userService.register(userParam));
    }
    @ApiOperation("删除")
    @DeleteMapping(value = "/delete")
    @PreAuthorize("hasAuthority('管理员')")
    public CommonResult<Integer> delete(@RequestBody List<Long> ids) {
        return CommonResult.success(userService.delete(ids));
    }
    @ApiOperation("列表")
    @PostMapping(value = "/list")
    @PreAuthorize("hasAuthority('管理员')")
    public CommonResult<CommonPage<User>> list(@RequestParam(defaultValue = "0",required = false) Integer pageNum,
                                         @RequestParam(defaultValue = "5",required = false) Integer pageSize,
                                         @RequestBody User user) {
        return CommonResult.success(CommonPage.restPage(userService.list(pageNum,pageSize,user)));
    }

    @ApiOperation("更新")
    @PutMapping(value = "/update")
    @PreAuthorize("hasAuthority('管理员')")
    public CommonResult<Integer> update(@RequestBody User user) {
        return CommonResult.success(userService.update(user));
    }
}
